package com.dehong.controller;

import com.dehong.model.ContractProductC;
import com.dehong.model.FactoryC;
import com.dehong.service.ContractCService;
import com.dehong.service.ContractProductCService;
import com.dehong.service.FactoryCService;
import com.dehong.vo.ContractCVo;
import com.dehong.vo.ContractPCVo;
import com.dehong.vo.ShipVo;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class ContractPCController {

@Autowired
private ContractProductCService contractProductCService;
    @Autowired
    private FactoryCService factoryCService;
    @Autowired
    private ContractCService contractCService;
    @InitBinder
    public  void init(WebDataBinder binder){
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }
@RequestMapping("selectPC")
    public  String selectPC(Model model,String contractId,HttpServletRequest request){
    List<ContractPCVo> cList = contractProductCService.selectCP();
    List<FactoryC> selectfactory = factoryCService.selectfactory();
    request.getSession().setAttribute("cid",contractId);
    for (ContractPCVo c: cList) {
        System.out.println(c);
    }
    model.addAttribute("list",selectfactory);

    model.addAttribute("dataList",cList);

    return "/pages/basicinfo/contractpc/ContracPCtList";
}
@RequestMapping("insertContractPc")
    public  String insertContractPc(ContractProductC contractProductC,String contractId,HttpServletRequest request){
    contractProductC.setContractProductId(UUID.randomUUID().toString());
    byte[] bt = new byte[1024];
    contractProductC.setContractId(contractId);
    contractProductC.setAccessories(bt);
    int i = contractProductCService.insertSelective(contractProductC);
    if (i>0){
      request.getSession().setAttribute("cid",contractId);

        return "redirect:/selectPC.action";

    }else {
        return "error";
    }
}
@RequestMapping("goupdateContractPc")
    public String goupdateContractPc(String contractProductId,Model model){
    System.out.println(contractProductId);
    ContractProductC contractProductC = contractProductCService.selectByPrimaryKey(contractProductId);
    List<FactoryC> poVos = factoryCService.selectfactory();
    model.addAttribute("cc",poVos);
    model.addAttribute("list",contractProductC);
      return  "/pages/basicinfo/contractpc/ContractPCUpdate";
}
    @RequestMapping("updateContractPc")
    public  String updateContractPc(ContractProductC contractProductC){
        int i = contractProductCService.updateByPrimaryKeySelective(contractProductC);
        if (i>0){
            return "redirect:/selectPC.action";
        }else {
            return "error";
        }
    }
    @RequestMapping("deleteContractPc")
    public  String deleteContractPc(String contractProductId){
        int i = contractProductCService.deleteByPrimaryKey(contractProductId);
        if (i>0){
            return "redirect:/selectPC.action";
        }else {
            return "error";
        }
    }
    //
    @RequestMapping("goshipmentselect")
    public  String goshipmentselect(){
    return  "/pages/basicinfo/contractpc/ship";
    }
    //出货打印
    @RequestMapping("shipmentselect")
    public void shipmentselect(ShipVo shipTime, HttpServletResponse response) throws IOException {

        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        List<ShipVo> list = contractCService.selectByShipTime(shipTime);
        for (ShipVo shipVo : list) {
            System.out.println(list);
        }
        //创建excel文件

        HSSFWorkbook wb=new HSSFWorkbook();
        //创建sheet也

        HSSFSheet sheet=wb.createSheet("客户信息表");
        int rowNo = 0;							//行号
        int colNo = 1;
        Row nRow = null;
        Cell nCell = null;
        //合并的单元格样式
        HSSFCellStyle Style = wb.createCellStyle();
        Style.setAlignment(HorizontalAlignment.CENTER);
        HSSFRow title=sheet.createRow(0);
        nRow = sheet.createRow(rowNo++);
        nRow.setHeightInPoints(36);

        nCell = nRow.createCell(1);


        nCell.setCellValue("2020-1-1".replaceFirst("-0", "-").replaceFirst("-", "年") + "月份出货表");		//yyyy-MM


        //创建标题行
        HSSFRow hr=sheet.createRow(1);
        hr.createCell(0).setCellValue("序号");
        hr.createCell(1).setCellValue("客户名称");
        hr.createCell(2).setCellValue("厂名");
        hr.createCell(3).setCellValue("船期");
        hr.createCell(4).setCellValue("工厂交期");
        hr.createCell(5).setCellValue("打印版式");
        hr.createCell(6).setCellValue("状态");
        hr.createCell(7).setCellValue("订单号");
        hr.createCell(8).setCellValue("货号");
        hr.createCell(9).setCellValue("贸易条款");
        int excl_id=0;
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
        for (ShipVo shipVo : list) {
            excl_id++;
            HSSFRow datarow=sheet.createRow(sheet.getLastRowNum()+1);
            datarow.createCell(0).setCellValue(excl_id);
            datarow.createCell(1).setCellValue(shipVo.getCustomName());
            datarow.createCell(2).setCellValue(shipVo.getFullName());
            datarow.createCell(3).setCellValue(sim.format(shipVo.getShipTime()));
            datarow.createCell(4).setCellValue(sim.format(shipVo.getDeliveryPeriod()));
            datarow.createCell(5).setCellValue(shipVo.getPrintStyle());

            datarow.createCell(6).setCellValue(shipVo.getState().intValue());
            datarow.createCell(7).setCellValue(shipVo.getOrderNo().intValue());
            datarow.createCell(8).setCellValue(shipVo.getCnumber().intValue());

            response.setContentType("application/octet-stream;charset=utf-8");
            try {
                response.setHeader("Content-Disposition", "attachment;filename=" + new String("客户名单".getBytes(),"iso-8859-1") + ".xls");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            OutputStream ouputStream = response.getOutputStream();
            if (excl_id==list.size()){
                wb.write(ouputStream);
                ouputStream.flush();
                ouputStream.close();
            }
        }
    }
}
